var app = getApp()
var tabbar = require('../../utils/tabbar');
import _config from '../../utils/config';
import _fetch from '../../utils/httpService';

Page({
  data: {
    userName: '',
    teamName: '',
    userWork: '',
    companyName: '',
    userImg: '',
    hangeShow: false,
    enterpriseId: null,
    companyName: '',
    companyList: [],
    companyHeight: '100px'
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    if (app.globalData.isService) {
      tabbar.tabbar("tabBar", 2, this)
    } else {
      tabbar.tabbar("tabBar", 1, this, 0)
    }
    this.setData({
      enterpriseId: app.globalData.enterpriseId
    })
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    this.getUserData()
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏
  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
  },
  onShareAppMessage: function () {
    // 用户点击右上角分享
    return {
      title: '助力企业工单管理智能升级', // 分享标题
      desc: '', // 分享描述
      path: '/pages/login/index', // 分享路径
      imageUrl: '../../img/Mask.png' // 分享图片
    }
  },
  toChangeCompany() {
    this.setData({
      changeShow: true
    })
  },
  closeDialog() {
    this.setData({
      changeShow: false
    })
  },
  getUserData() {
    // 获取用户信息
    let user = app.globalData.userData
    console.log(user)
    let clist = app.globalData.userData.enterprises
    let hpx = clist.length < 4 ? clist.length * 108 : 420
    let teamsName = []
    if (user.staff.serviceTeams && user.staff.serviceTeams.length > 0) {
      teamsName = user.staff.serviceTeams.map(i => {
        return i.teamName
      })
    }
    console.log(teamsName)
    this.setData({
      userName: user.staff.name,
      teamName: teamsName.join('、'),
      userWork: user.staff.jobTitle,
      userImg: user.staff.headImg,
      companyName: user.staff.enterprise.name,
      companyList: clist,
      companyHeight: `${hpx}rpx`
    })
  },
  getPermsList() {
    // 获取权限
    _fetch.get(_config.url.get_perms)
      .then(function (res) {
        console.log(res)
        let permArr = []
        if (res.indexOf('IFORM_ORDER_ACCEPT') > -1) {
          permArr.push('accept', 'refuse') // 受理、拒绝
        }
        if (res.indexOf('IFORM_ORDER_ASSIGN') > -1) {
          permArr.push('assignForm', 'transferForm') // 指派、转派
        }
        if (res.indexOf('IFORM_ORDER_SCRAMBLE') > -1) {
          permArr.push('grabForm') // 抢单
        }
        if (res.indexOf('IFORM_ORDER_RECEIVE') > -1) {
          permArr.push('receiveForm', 'refuseForm') // 接受、拒绝
        }
        if (res.indexOf('IFORM_ORDER_SERVICE') > -1) {
          permArr.push('startService', 'applyException', 'writeProgress', 'completeService') // 开始服务、服务完成、填写进度、申请异常
        }
        if (res.indexOf('IFORM_ORDER_HANDLE_EXCEPTION') > -1) {
          permArr.push('agreeApply', 'rejectApply') // 同意、驳回
        }
        if (res.indexOf('IFORM_ORDER_MESSAGE') > -1) {
          permArr.push('formMessage') // 工单留言
        }
        if (res.indexOf('IFORM_ORDER_CANCEL') > -1) {
          permArr.push('cancelForm') // 取消工单
        }
        if (res.indexOf('IFORM_ORDER_EVALUATE') > -1) {
          permArr.push('evaluateForm') // 评价工单
        }
        console.log(permArr)
        getApp().globalData.perms = permArr;
      }).catch(function (error) {
        wx.showToast({
          title: error.msg,
          icon: 'none',
        });
      });
  },
  hideCatchTabBar(res) {
    // 服务人员不展示接抢单tab
    getApp().globalData.isService = false
    if (res.staff.serviceTeams && res.staff.serviceTeams.length > 0) {
      res.staff.serviceTeams.map(i => {
        if (i.roleInfoOfServiceTeamList && i.roleInfoOfServiceTeamList.length > 0) {
          i.roleInfoOfServiceTeamList.map(j => {
            if (j.code === 'SERVICE_PERSONAL') {
              getApp().globalData.isService = true
            }
          })
        }
      })
    }
    setTimeout(() => {
      if (app.globalData.isService) {
        tabbar.tabbar("tabBar", 2, this)
      } else {
        tabbar.tabbar("tabBar", 1, this, 0)
      }
      wx.redirectTo({
        url: '../mine/index'
      })
    }, 0);
  },
  enterpriseSwitch(e) {
    // 切换企业
    if (this.data.companyList.length > 1) {
      let vm = this
      let name = e.currentTarget.dataset.ename
      getApp().globalData.enterpriseId = e.currentTarget.dataset.eid
      let url = _config.url.choose_enterprise.replace('{id}', e.currentTarget.dataset.eid);
      _fetch.get(url)
        .then(function (res) {
          getApp().globalData.userData = res;
          vm.hideCatchTabBar(res)
          vm.getPermsList()
          vm.closeDialog()
          let clist = app.globalData.userData.enterprises
          let hpx = clist.length < 4 ? clist.length * 108 : 420
          vm.setData({
            companyName: name,
            companyList: clist,
            companyHeight: `${hpx}rpx`,
            enterpriseId: e.currentTarget.dataset.eid
          })
          vm.getUserData()
        }).catch(function (error) {
          wx.showToast({
            title: error.msg,
            icon: 'none',
          });
        });
    }
  },
  changePhone() {
    // 改绑手机
    wx.navigateTo({
      url: '../changePhone/index'
    })
  },
  toFeedback() {
    // 意见反馈
    wx.navigateTo({
      url: '../feedBack/index'
    })
  }
})